package com.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     * @param userName
     * @param userPwd
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public UserBean login(String userName, String userPwd) {
        UserBean userBean = userService.login(userName, userPwd);
        if (userBean != null) {
            return userBean;
        }
        return new UserBean();
    }

    /**
     * 展示信息
     * @param pageNO
     * @param pageSize
     * @return
     */
    @RequestMapping("findAllUser")
    @ResponseBody
    public IPage<UserBean> findAllUser(@RequestParam(value = "pageNO", defaultValue = "1") int pageNO,
                                       @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        return userService.findAllUser(pageNO, pageSize);
    }

    /**
     * 添加
     * @param userBean
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(UserBean userBean) {
        userService.addUser(userBean);
        return "添加成功";
    }


    /**
     * 删除用户
     */
    @RequestMapping("/delUser")
    @ResponseBody
    public String delUser(int userId) {
        userService.delUser(userId);
        return "删除成功";
    }


    /**
     * 修改用户
     * @param userId
     * @param userPwd
     * @param userRood
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(int userId, String userPwd, String userRood) {
        userService.updateUser(userId, userPwd, userRood);
        return "修改成功";
    }


    /**
     * 模糊查询
     * @param pageNO
     * @param pageSize
     * @param userRood
     * @return
     */
    @RequestMapping("/findGrade")
    @ResponseBody
    public IPage<UserBean> findRood(@RequestParam(value = "pageNO", defaultValue = "1") int pageNO,
                                     @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                                     String userRood) {
        return userService.findRood(pageNO, pageSize, userRood);
    }

}
